package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.riddle.lib.model.RiddleConst;
import com.stark.riddle.lib.model.RiddlePrefUtil;
import com.stark.riddle.lib.model.bean.Twister;
import com.stark.riddle.lib.model.db.RiddleDaoHelperManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityTwisterBinding;
import flc.ast.utils.Content;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.inf.IRetCallback;
import stark.common.basic.utils.RandomUtil;
import stark.common.basic.utils.SPUtil;

public class TwisterActivity extends BaseAc<ActivityTwisterBinding> {

    public static String kindName;
    public static int selLevel = 0;//选择的关卡
    private List<Twister> listData = new ArrayList<>();
    private int rightNum;
    private boolean isPass = false;
    private Dialog myPassDialog;
    private TextView dialogTitle;
    private ImageView dialogImg, dialogNext;


    @Override
    protected int onCreate() {
        return R.layout.activity_twister;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivTwisterBack.setOnClickListener(this);

        mDataBinding.ivTwisterTip.setOnClickListener(this);
        mDataBinding.rlTwisterOptionA.setOnClickListener(this);
        mDataBinding.rlTwisterOptionB.setOnClickListener(this);
        mDataBinding.rlTwisterOptionC.setOnClickListener(this);
        mDataBinding.rlTwisterOptionD.setOnClickListener(this);

        //通关弹框
        passDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivTwisterBack://返回首页
                startActivity(HomeActivity.class);
                break;
            case R.id.ivTwisterTip://提示
                String tip = "";
                switch (rightNum) {
                    case 1:
                        tip = mDataBinding.tvTwisterOptionA.getText().toString();
                        break;
                    case 2:
                        tip = mDataBinding.tvTwisterOptionB.getText().toString();
                        break;
                    case 3:
                        tip = mDataBinding.tvTwisterOptionC.getText().toString();
                        break;
                    case 4:
                        tip = mDataBinding.tvTwisterOptionD.getText().toString();
                        break;
                }
                ToastUtils.showShort(tip);
                break;
            case R.id.rlTwisterOptionA://选项A
                isRight(1, mDataBinding.rlTwisterOptionA, mDataBinding.tvTwisterOptionA.getText().toString());
                break;
            case R.id.rlTwisterOptionB://选项B
                isRight(2, mDataBinding.rlTwisterOptionB, mDataBinding.tvTwisterOptionB.getText().toString());
                break;
            case R.id.rlTwisterOptionC://选项C
                isRight(3, mDataBinding.rlTwisterOptionC, mDataBinding.tvTwisterOptionC.getText().toString());
                break;
            case R.id.rlTwisterOptionD://选项D
                isRight(4, mDataBinding.rlTwisterOptionD, mDataBinding.tvTwisterOptionD.getText().toString());
                break;
            //过关弹框
            case R.id.ivDialogPassBack://返回首页
                myPassDialog.dismiss();
                startActivity(HomeActivity.class);
                break;
            case R.id.ivDialogPassNext://重来/下一关
                myPassDialog.dismiss();
                if (isPass) {
                    if (selLevel == (listData.size() - 1)) {
                        ToastUtils.showShort(R.string.it_is_already_the_last_level);
                        return;
                    }
                    int spLevel = RiddlePrefUtil.getUnlockCheckPoint(RiddleConst.FuncType.TWISTER, kindName);
                    if (selLevel == (spLevel - 1)) {//如果通过的是未解锁关卡则解锁下一关
                        RiddlePrefUtil.saveUnlockCheckPoint(RiddleConst.FuncType.TWISTER, kindName, spLevel + 1);
                    }
                    selLevel++;
                    setData();
                } else {
                    mDataBinding.rlTwisterOptionA.setBackgroundResource(R.drawable.xxjx);
                    mDataBinding.rlTwisterOptionB.setBackgroundResource(R.drawable.xxjx);
                    mDataBinding.rlTwisterOptionC.setBackgroundResource(R.drawable.xxjx);
                    mDataBinding.rlTwisterOptionD.setBackgroundResource(R.drawable.xxjx);
                }
                break;
        }
    }

    @Override
    protected void initData() {
        RiddleDaoHelperManager.getTwisterDbHelper().getByKind(kindName, 0, 80, new IRetCallback<List<Twister>>() {
            @Override
            public void onResult(List<Twister> result) {
                if (result != null && result.size() > 0) {
                    listData.addAll(result);
                    setData();
                }
            }
        });
    }

    //设置数据
    private void setData() {
        //恢复选项初始状态
        isPass = false;
        mDataBinding.rlTwisterOptionA.setBackgroundResource(R.drawable.xxjx);
        mDataBinding.rlTwisterOptionB.setBackgroundResource(R.drawable.xxjx);
        mDataBinding.rlTwisterOptionC.setBackgroundResource(R.drawable.xxjx);
        mDataBinding.rlTwisterOptionD.setBackgroundResource(R.drawable.xxjx);
        //设置游戏数据
        mDataBinding.tvTwisterProblem.setText(listData.get(selLevel).getTwisterDes());
        //生成选项
        List<Twister> twisters = RandomUtil.randomGetItems(listData, listData.get(selLevel), 3);
        //显示选项
        mDataBinding.tvTwisterOptionA.setText(twisters.get(0).getTwisterKey());
        mDataBinding.tvTwisterOptionB.setText(twisters.get(1).getTwisterKey());
        mDataBinding.tvTwisterOptionC.setText(twisters.get(2).getTwisterKey());
        mDataBinding.tvTwisterOptionD.setText(twisters.get(3).getTwisterKey());

        boolean isA = checkAnswer(twisters.get(0).getTwisterKey(), listData.get(selLevel).getTwisterKey());
        boolean isB = checkAnswer(twisters.get(1).getTwisterKey(), listData.get(selLevel).getTwisterKey());
        boolean isC = checkAnswer(twisters.get(2).getTwisterKey(), listData.get(selLevel).getTwisterKey());
        boolean isD = checkAnswer(twisters.get(3).getTwisterKey(), listData.get(selLevel).getTwisterKey());
        if (isA) {
            rightNum = 1;
        }
        if (isB) {
            rightNum = 2;
        }
        if (isC) {
            rightNum = 3;
        }
        if (isD) {
            rightNum = 4;
        }
    }

    //判断答案是否正确
    private boolean checkAnswer(String s1, String s2) {
        if (s1.equals(s2)) {
            return true;
        }
        return false;
    }

    //判断对错
    private void isRight(int kind, RelativeLayout viewBg, String rightAnswer) {
        if (kind == rightNum) {
            viewBg.setBackgroundResource(R.drawable.zq);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    isPass = true;
                    dialogTitle.setText(getString(R.string.level_num, selLevel + 1));
                    dialogImg.setImageResource(R.drawable.gxgg);
                    dialogNext.setImageResource(R.drawable.xyy);
                    myPassDialog.show();
                }
            }, 200);
        } else {
            viewBg.setBackgroundResource(R.drawable.cw);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    isPass = false;
                    dialogTitle.setText(getString(R.string.level_num, selLevel + 1));
                    dialogImg.setImageResource(R.drawable.tzsb);
                    dialogNext.setImageResource(R.drawable.zsyc);
                    myPassDialog.show();
                }
            }, 200);
        }
    }

    //通关弹框
    private void passDialog() {
        myPassDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_pass, null);
        myPassDialog.setContentView(inflate);
        myPassDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myPassDialog.getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 1.0);
        lp.height = dm.heightPixels;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        ImageView dialogBack = inflate.findViewById(R.id.ivDialogPassBack);
        dialogTitle = inflate.findViewById(R.id.tvDialogPassTitle);
        dialogImg = inflate.findViewById(R.id.ivDialogPassImg);
        dialogNext = inflate.findViewById(R.id.ivDialogPassNext);
        dialogBack.setOnClickListener(this);
        dialogNext.setOnClickListener(this);
    }

}